package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.cskaoyan.model.Coupon;
import com.cskaoyan.model.CouponExample;
import com.cskaoyan.model.CouponUser;
import com.cskaoyan.model.CouponUserExample;
import com.cskaoyan.model.bo.CouponListBO;
import com.cskaoyan.model.bo.CouponUserListBO;
import com.cskaoyan.model.vo.CouponInfoVO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.service.CouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @ClassName: CouponServiceImpl
 * @Description: 优惠券模块实现类
 * @Author:YPS
 * @Date: 2022/5/6 12:00
 * @Version: 1.0
 **/
@Service
@Transactional
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;
    //按条件查询优惠券
    @Override
    public DataVO<CouponInfoVO> queryCoupon(CouponListBO couponListBO) {
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        couponExample.setOrderByClause(couponListBO.getSort() + " " + couponListBO.getOrder());
        if (!(couponListBO.getName() == null||"".equals(couponListBO.getName()))){
            criteria.andNameLike("%" + couponListBO.getName() + "%");
        }
        if (!(couponListBO.getType()==null||"".equals(couponListBO.getType()))){
            criteria.andTypeEqualTo(couponListBO.getType());
        }
        if(!(couponListBO.getStatus()==null||"".equals(couponListBO.getStatus()))){
            criteria.andStatusEqualTo(couponListBO.getStatus());
        }
        PageHelper.startPage(couponListBO.getPage(), couponListBO.getLimit());
        List<Coupon> couponList = couponMapper.selectByExample(couponExample);
        PageInfo<Coupon> pageInfo = new PageInfo<>(couponList);
        long total = pageInfo.getTotal();

        List<CouponInfoVO> couponInfoVOS = new ArrayList<>();
        for (Coupon coupon : couponList) {
            CouponInfoVO couponInfoVO = new CouponInfoVO();
            couponInfoVO.setId(coupon.getId());
            couponInfoVO.setAddTime(coupon.getAddTime());
            couponInfoVO.setDays(coupon.getDays());
            couponInfoVO.setDeleted(coupon.getDeleted());
            couponInfoVO.setDesc(coupon.getDesc());
            couponInfoVO.setDiscount(coupon.getDiscount());
            couponInfoVO.setGoodsType(coupon.getGoodsType());
            couponInfoVO.setGoodsValue(coupon.getGoodsValue());
            couponInfoVO.setLimit(coupon.getLimit());
            couponInfoVO.setMin(coupon.getMin());
            couponInfoVO.setName(coupon.getName());
            couponInfoVO.setStatus(coupon.getStatus());
            couponInfoVO.setTag(coupon.getTag());
            couponInfoVO.setTimeType(coupon.getTimeType());
            couponInfoVO.setTotal(coupon.getTotal());
            couponInfoVO.setType(coupon.getType());
            couponInfoVO.setUpdateTime(coupon.getUpdateTime());
            couponInfoVOS.add(couponInfoVO);
        }
        DataVO<CouponInfoVO> couponInfoVODataVO = new DataVO<>();
        couponInfoVODataVO.setPages(pageInfo.getPages());
        couponInfoVODataVO.setPage(couponListBO.getPage());
        couponInfoVODataVO.setTotal((int) total);
        couponInfoVODataVO.setLimit(couponListBO.getLimit());
        couponInfoVODataVO.setList(couponInfoVOS);
        return couponInfoVODataVO;
    }

    //新增优惠券
    @Override
    public Coupon createCoupon(Coupon couponInfo) {
        if (couponInfo.getType() == 2){
            String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            Random random = new Random();
            Boolean hasNum = false;
            Boolean hasLetter = false;
            String code = "";
            for (int j = 0; j < 8; j++) {
                Integer i = random.nextInt(36);
                String s1 = String.valueOf(s.charAt(i));
                if (i <= 25){
                    hasLetter = true;
                }else {
                    hasNum = true;
                }
                if (j == 7){
                    if (hasLetter == false){
                        Integer i2 = random.nextInt(25);
                        code += String.valueOf(s.charAt(i2));
                        break;
                    }else if(hasNum == false){
                        Integer i3 = random.nextInt(10) + 26;
                        code += String.valueOf(s.charAt(i3));
                        break;
                    }
                    code += s1;
                    break;
                }
                code += s1;
            }
            couponInfo.setCode(code);
        }
        Date date = new Date();
        couponInfo.setAddTime(date);
        couponInfo.setUpdateTime(date);
        couponInfo.setDeleted(false);
        couponMapper.insertSelective(couponInfo);
        return couponInfo;
    }
    //编辑优惠券
    @Override
    public CouponInfoVO updateCoupon(Coupon coupon) {
        CouponInfoVO couponInfoVO = new CouponInfoVO();
        Date date = new Date();
        coupon.setUpdateTime(date);
        System.out.println(coupon.getName());

        couponMapper.updateByPrimaryKeySelective(coupon);
        couponInfoVO.setId(coupon.getId());
        couponInfoVO.setAddTime(coupon.getAddTime());
        couponInfoVO.setDays(coupon.getDays());
        couponInfoVO.setDeleted(coupon.getDeleted());
        couponInfoVO.setDesc(coupon.getDesc());
        couponInfoVO.setDiscount(coupon.getDiscount());
        couponInfoVO.setGoodsType(coupon.getGoodsType());
        couponInfoVO.setGoodsValue(coupon.getGoodsValue());
        couponInfoVO.setLimit(coupon.getLimit());
        couponInfoVO.setMin(coupon.getMin());
        couponInfoVO.setName(coupon.getName());
        couponInfoVO.setStatus(coupon.getStatus());
        couponInfoVO.setTag(coupon.getTag());
        couponInfoVO.setTimeType(coupon.getTimeType());
        couponInfoVO.setTotal(coupon.getTotal());
        couponInfoVO.setType(coupon.getType());
        couponInfoVO.setUpdateTime(coupon.getUpdateTime());
        return couponInfoVO;
    }
    //删除优惠券
    @Override
    public void deleteCoupon(Coupon coupon) {
        coupon.setDeleted(true);
        couponMapper.updateByPrimaryKeySelective(coupon);
    }

    @Override
    public Coupon queryCouponById(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    @Override
    public DataVO<CouponUser> getUserList(CouponUserListBO couponUserListBO) {
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        couponUserExample.setOrderByClause(couponUserListBO.getSort()+" " + couponUserListBO.getOrder());
        criteria.andDeletedEqualTo(false);
//        if ((couponUserListBO.getUserId()==null||"".equals(couponUserListBO.getUserId()))&&(couponUserListBO.getStatus() == null||"".equals(couponUserListBO.getStatus()))){
//            criteria.andCouponIdEqualTo(couponUserListBO.getCouponId());
//            List<CouponUser> couponUserList = couponUserMapper.selectByExample(couponUserExample);
//            DataVO<CouponUser> couponUserDataVO = new DataVO<>();
//            couponUserDataVO.setLimit(couponUserListBO.getLimit());
//            couponUserDataVO.setPage(couponUserListBO.getPage());
//            couponUserDataVO.setTotal(couponUserList.size());
//            couponUserDataVO.setPages(couponUserList.size()/couponUserListBO.getLimit() + 1);
//            couponUserDataVO.setList(couponUserList);
//            return couponUserDataVO;
//        }
        if(!(couponUserListBO.getUserId()==null||"".equals(couponUserListBO.getUserId()))){
            criteria.andUserIdEqualTo(couponUserListBO.getUserId());
        }
        if (!(couponUserListBO.getCouponId() == null||"".equals(couponUserListBO.getCouponId()))){
            criteria.andCouponIdEqualTo(couponUserListBO.getCouponId());
        }
        if (!(couponUserListBO.getStatus() == null || "".equals(couponUserListBO.getStatus()))){
            criteria.andStatusEqualTo(couponUserListBO.getStatus());
        }
        PageHelper.startPage(couponUserListBO.getPage(), couponUserListBO.getLimit());
        List<CouponUser> couponUserList = couponUserMapper.selectByExample(couponUserExample);
        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>(couponUserList);
        long total = couponUserPageInfo.getTotal();
        DataVO<CouponUser> couponUserDataVO = new DataVO<>();
        couponUserDataVO.setLimit(couponUserListBO.getLimit());
        couponUserDataVO.setPage(couponUserListBO.getPage());
        couponUserDataVO.setTotal((int) total);
        couponUserDataVO.setPages(couponUserPageInfo.getPages());
        couponUserDataVO.setList(couponUserList);
        return couponUserDataVO;
    }
}
